﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using Silvermoon.Controls;
using OutlookHub.Utils;
using Microsoft.WindowsMobile.PocketOutlook;
using Silvermoon.OpenGL;
using Silvermoon.OpenGL.Native;
using System.Drawing;
using OutlookHub.Images;
using Silvermoon.Windows;

namespace OutlookHub.Controls
{
    public class ContactTitleItem : ContainerItem
    {
        private Label label;
        private Label details;
        private PictureBox picture;
        private Texture texture;

        public ContactTitleItem(Contact contact)
            : base()
        {
            TextColor = Color.White;
            label = new Label { Font = FontPool.MediumRegularFont, LineMode = LineMode.WordWrap, AutoSize = true, Margin = new Margin(5, 5, 128 + 10, 5) };
            details = new Label { LineMode = LineMode.WordWrap, AutoSize = true, Margin = new Margin(5, 5, 128 + 10, 15), Opacity = 220 };
            picture = new PictureBox { Width = 128, Height = 128, Margin = new Margin(null, 5, 5, null)};
            StackPanel stackPanel = new StackPanel { Orientation = StackPanelOrientation.Vertical, Margin = new Margin(0), Alignment = StackPanelAlignment.Dense };
            stackPanel.Controls.Add(label, details);
            AutoSizePanel panel = new AutoSizePanel { Margin = new Margin(10, 10, 10, null) };
            this.Control = panel;
            panel.Controls.Add(stackPanel, picture);
            AutoHeight = true;
            Background = PhoneColors.FromArgb(64, 255, 255, 255);
            Margin = new Margin(0, 0, 0, 24);
            SetContact(contact);
        }

        protected override void Dispose(bool dispose)
        {
            DisposeObject(texture);
            base.Dispose(dispose);
        }



        private void SetContact(Contact contact)
        {
            if (contact != null)
            {
                label.Text = contact.FileAs;
                picture.Image = contact.Picture != null ? CreateImage(contact) : ImagePool.Contact;
                StringBuilder sb = new StringBuilder();
                sb.Append(contact.CompanyName);
                if (sb.Length > 0) sb.AppendLine();
                sb.Append(contact.JobTitle);
                details.Text = sb.ToString();
            }
        }

        private Texture CreateImage(Contact contact)
        {
            Image img = contact.Picture;
            Texture texture = new Texture();
            int w = img.Width;
            int h = img.Height;
            using (DrawingCanvas canvas = texture.CreateCanvas(128, 128, TextureFormat.RGBA5551, Color.Fuchsia))
            {
                Graphics gfx = canvas.Graphics;
                gfx.Clear(Color.Fuchsia);
                int w0 = 128 * w / h;
                int x = 128 - w0;
                gfx.DrawImage(img, new Rectangle(x, 0, w0, 128), new Rectangle(0, 0, w, h), GraphicsUnit.Pixel);
            }
            this.texture = texture;

            return texture;
        }
    }
}
